<?php
/**
 * @file productcontroller.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2017-03-06 下午17:53:07 $
 * @brief  $
 */
namespace haibao\cms\business\control\pc\channel\product;

use haibao\cms\data\control\ProductController as ProductControllerData;
use \haibao\cms\model\filter\newproduct\Product as ProductFilter;
use \haibao\cms\model\filter\newproduct\ProductBrandGroup as ProductBrandGroupFilter;
use \haibao\cms\model\filter\newproduct\ProductCategoryRelation as CategoryRelationFilter;
use \haibao\cms\model\filter\newproduct\Goods as GoodsModelFilter;
use \haibao\frame\data\query\Condition;
use haibao\cms\model\filter\TryProduct as TryProductFilter;
use haibao\cms\business\TryProduct as TryProductBusiness;

class ProductController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const PAGE_SIZE = 30;
	private static $_instance;
	public $productData;
	public function __construct($updateTime='',$controlId='',$isPublish=''){
		if(!$this->productData instanceof ProductController){
			$this->productData = new ProductControllerData;
		}
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	public static function getProductInstance(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	
	public static function getNewGoodsList(){
	    $goodsModelFilter = new GoodsModelFilter();
	    $goodsModelFilter->select(array(
	        GoodsModelFilter::CONDITION_FIELD_NAME_ID,
	        GoodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
	    ))->limit(200);
	    
	    $goodsModelFilter->where(GoodsModelFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\newproduct\Product::STATUS_ENABLE);
	    $goodsModelFilter->where(GoodsModelFilter::CONDITION_FIELD_NAME_ISEDIT, Condition::CONDITION_EQUAL, \haibao\cms\model\data\newproduct\Product::STATUS_ENABLE);
	
	    $goodsImageModelFilter = new \haibao\cms\model\filter\newproduct\GoodsImage();
	    $goodsImageModelFilter->select(array(
	        $goodsImageModelFilter::CONDITION_FIELD_NAME_IMAGE_ID
	    ));
	    $goodsImageModelFilter->where($goodsImageModelFilter::CONDITION_FIELD_NAME_IS_COVER, Condition::CONDITION_EQUAL, \haibao\cms\model\data\newproduct\GoodsImage::IS_COVER_YES);
	    
	    $goodsModelFilter->leftJoin($goodsImageModelFilter,null,$goodsModelFilter::CONDITION_FIELD_NAME_ID,$goodsImageModelFilter::CONDITION_FIELD_NAME_GOODS_ID,$goodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_GOODS_INFO,false);
	   
	    $goodsModelFilter->order($goodsModelFilter::CONDITION_FIELD_NAME_CREATETIME,false);
	    
	    $goodsList = \haibao\cms\business\newproduct\Goods::getModelList($goodsModelFilter);
	    if($goodsList){
	        $imageIds = array();
            foreach($goodsList as $key=>$product){
                $imageIds[] = $product->ProductGoodsInfo->ImageId;
            }
            $imageUrls = \haibao\cms\business\ImageLibrary::getImageUrlById($imageIds);
	        foreach($goodsList as $key=>$product){
                $goodsList[$key]->PicUrl = self::getImageLink($imageUrls[$product->ProductGoodsInfo->ImageId],235,235);
	        }
	    }
	    return $goodsList;
	}
	
	public static function getNewFashionGoodsList($categoryParentIdArr = array()){
        $resultData = array();
	    if($categoryParentIdArr){
	        foreach($categoryParentIdArr as $k=>$val){
	            $childIds = array();
	            foreach($val['Child'] as $childList){
	                if($childList['ChildList']){
                        $childIds = array_merge($childIds,array_keys($childList['ChildList']));
                    }
	            }

                $goodsModelFilter = new GoodsModelFilter();
                $goodsModelFilter->select(array(
                    GoodsModelFilter::CONDITION_FIELD_NAME_ID,
                    GoodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_ID,
                    GoodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_NAME,
                ))->limit(4);

                $productFilter = new ProductFilter();
                $productFilter->select(array(
                    ProductFilter::CONDITION_FIELD_NAME_ID,
                    ProductFilter::CONDITION_FIELD_NAME_PRODUCT_SERIES,
                    ProductFilter::CONDITION_FIELD_NAME_SEASON,
                    ProductFilter::CONDITION_FIELD_NAME_SEASON_YEAR,
                ));

                $goodsModelFilter->leftJoin($productFilter,null,GoodsModelFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_ID,GoodsModelFilter::CONDITION_FIELD_NAEM_PRODUCT,false);

//                $productBrandFilter = new ProductBrandGroupFilter();
//                $productBrandFilter->select(array(
//                    ProductBrandGroupFilter::CONDITION_FIELD_NAME_BRAND_ID,
//                ));
//                $goodsModelFilter->leftJoin($productBrandFilter,$productFilter,ProductFilter::CONDITION_FIELD_NAME_ID,ProductBrandGroupFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_BRAND_IDS);

                $categoryRelationFilter = new CategoryRelationFilter();
                $categoryRelationFilter->select(array(
                    CategoryRelationFilter::CONDITION_FIELD_NAME_ID,
                    CategoryRelationFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
                ));
                $categoryRelationFilter->where($categoryRelationFilter::CONDITION_FIELD_NAME_CATEGORY_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $childIds);
                $goodsModelFilter->leftJoin($categoryRelationFilter,$productFilter,ProductFilter::CONDITION_FIELD_NAME_ID,CategoryRelationFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_CATEGORY_IDS,false);


                $goodsModelFilter->where(GoodsModelFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Product::STATUS_ENABLE);
                $goodsModelFilter->where(GoodsModelFilter::CONDITION_FIELD_NAME_ISEDIT, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Product::STATUS_ENABLE);
                
                $goodsModelFilter->order(GoodsModelFilter::CONDITION_FIELD_NAME_CREATETIME,false);

                $goodsList = \haibao\cms\business\newproduct\Goods::getModelList($goodsModelFilter);

                if($goodsList){
                    $goodsIds = $brandIds = $productIds = array();
                    foreach($goodsList as $key=>$val){
                        $goodsIds[] = $val->Id;
                        $productIds[] = $val->ProductId;
                    }

                    $productFilter = new ProductFilter();
                    $productFilter->select(array(
                        ProductFilter::CONDITION_FIELD_NAME_ID));
                    $productBrandFilter = new ProductBrandGroupFilter();
                    $productBrandFilter->select(array(
                        ProductBrandGroupFilter::CONDITION_FIELD_NAME_BRAND_ID,
                    ));
                    $productFilter->leftJoin($productBrandFilter,null,ProductFilter::CONDITION_FIELD_NAME_ID,ProductBrandGroupFilter::CONDITION_FIELD_NAME_PRODUCT_ID,ProductFilter::CONDITION_FIELD_NAME_BRAND_IDS);
                    $productFilter->where(ProductFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $productIds);
                    $productModels = \haibao\cms\business\newproduct\Product::getProduct($productFilter);

                    $productIds = array();
                    foreach ($productModels as $model){
                        if($model->BrandIds){
                            foreach ($model->BrandIds as $brandModel){
                                $brandIds[] = $brandModel->BrandId;
                                $productIds[$model->Id][] = $brandModel->BrandId;
                            }
                        }

                    }

                    $brandList = \haibao\cms\business\Brand::getBrandByIds($brandIds,true,true);
                    $imageInfo = \haibao\cms\business\newproduct\Goods::getImageIdByGoodsId($goodsIds);//单品图片
                    foreach($goodsList as $key=>$val){
                        foreach ($productIds[$val->ProductId] as $brandId){
                            if(!empty($brandList[$brandId])){
                                $val->BrandName[] = $brandList[$brandId];
                            }
                        }
                        if(isset($imageInfo[$val->Id]) && !empty($imageInfo[$val->Id])){
                            $val->PicUrl = self::getImageLink($imageInfo[$val->Id],235,235);;
                        }
                        $goodsList[$key] = $val;
                    }
                }

                $resultData[$k] = $goodsList;
	        }
	    }
	    return $resultData;
	}
	
	public static function getTryOut(){
	    $tryOn = new TryProductFilter();
	    $tryOn->select(array(
	        TryProductFilter::CONDITION_FIELD_NAME_ID,
	        TryProductFilter::CONDITION_FIELD_NAME_TITLE,
	        TryProductFilter::CONDITION_FIELD_NAME_INTRODUCE,
	        TryProductFilter::CONDITION_FIELD_NAME_BRAND_NAME,
	        TryProductFilter::CONDITION_FIELD_NAME_PIC_ID,
	        TryProductFilter::CONDITION_FIELD_NAME_SPECIFICATIONS,
	        TryProductFilter::CONDITION_FIELD_NAME_AMOUNT,
	        TryProductFilter::CONDITION_FIELD_NAME_PRICE,
	        TryProductFilter::CONDITION_FIELD_NAME_STATUS,
	        TryProductFilter::CONDITION_FIELD_NAME_PUBLISHED,
	        TryProductFilter::CONDITION_FIELD_NAME_APPLY_START_TIME,
	        TryProductFilter::CONDITION_FIELD_NAME_APPLY_END_TIME,
	    ));
	    
	    $tryOn->where(
	        TryProductFilter::CONDITION_FIELD_NAME_PUBLISHED,
	        Condition::CONDITION_EQUAL,
	        \haibao\cms\model\data\TryProduct::PUBLISHED_YES
	    );
	    $tryOn->where(
	        TryProductFilter::CONDITION_FIELD_NAME_STATUS,
	        Condition::CONDITION_EQUAL,
	        \haibao\cms\model\data\TryProduct::STATUS_ENABLE
	    );
	    
	    $date = date('Y-m-d H:i:s');
	    //$date = date('2015-05-01 00:00:00');//本地测试数据
	    $tryOn->where(TryProductFilter::CONDITION_FIELD_NAME_APPLY_START_TIME, Condition::CONDITION_LESSEQUAL, $date);
	    $tryOn->where(TryProductFilter::CONDITION_FIELD_NAME_APPLY_END_TIME, Condition::CONDITION_GREATEREQUAL, $date);
	    
	    $tryOn->order( TryProductFilter::CONDITION_FIELD_NAME_PUBLISHDATE, false );
	    $tryOn->order( TryProductFilter::CONDITION_FIELD_NAME_ID, false );
	    
	    $tryOnList = TryProductBusiness::getTryProductByQuery($tryOn);
	    self::getImageUrl($tryOnList, 340, 340);
	    self::setEndTime($tryOnList);
	    
	    return $tryOnList;
	}
	
	private static function getImageUrl(&$list, $width = null, $height = null){
	    if($list){
	        foreach ($list as $key=>&$item){
	            $PicId = is_array($item) ? $item['PicId'] : $item->PicId;
	            $imageInfo = \haibao\cms\business\ImageLibrary::getImageById($PicId);
	            $imgUrl = $imageInfo->Filename;
	
	            $imgUrl = strpos($imageInfo->Filename, \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::IMAGE_UPLOAD_BASE_PATH))===false ? ltrim($imageInfo->Filename, '/') : $imageInfo->Filename;
	
	            if(is_array($item)){
	                $item['PicUrl'] = $imgUrl;
	            }else{
	                $item->PicUrl = $imgUrl;
	            }
	        }
	    }
	}
	
	/**
	 * 格式化时间
	 */
	private static function setEndTime(&$tryList){
	    if($tryList){
	        foreach ($tryList as $key=>$list){
	            $endTime = $list->ApplyEndTime->format('Y-m-d H:i:s');
	            $list->ApplyEndTime = strtotime($endTime) - strtotime(date('Y-m-d H:i:s'));
	            $list->ApplyEndTime = ceil($list->ApplyEndTime/3600/24);
	        }
	    }
	}
}